package root.agent.demo.advisors;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.advisor.api.*;
import org.springframework.ai.chat.model.MessageAggregator;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
/**
 * 自定义扩展器，日志
 * **/
@Slf4j
public class MyLogAdvisor implements CallAroundAdvisor, StreamAroundAdvisor {
    @Override
    public AdvisedResponse aroundCall(AdvisedRequest advisedRequest, CallAroundAdvisorChain chain) {
        log.info("请求参数：{}", advisedRequest.userText());
        AdvisedResponse advisedResponse = chain.nextAroundCall(advisedRequest);
        log.info("响应结果：{}", advisedResponse.response().getResult());
        return advisedResponse;
    }

    @Override
    public Flux<AdvisedResponse> aroundStream(AdvisedRequest advisedRequest, StreamAroundAdvisorChain chain) {
        log.info("请求参数：{}", advisedRequest.userText());
        Flux<AdvisedResponse> advisedResponse = chain.nextAroundStream(advisedRequest);
        return new MessageAggregator().aggregateAdvisedResponse(advisedResponse,this::observeAfter);
    }

    private void observeAfter(AdvisedResponse advisedResponse) {
        log.info("响应结果：{}", advisedResponse.response().getResult());
    }

    @Override
    public String getName() {
        return MyLogAdvisor.class.getSimpleName();
    }

    @Override
    public int getOrder() {
        return 100;
    }
}
